<template>
  <!--操作栏：编辑对话框-->
  <el-dialog :model-value="visible" @close="dialogClose" width="30%">
    <!--标题-->
    <template #header>
      <div style="font-size:18px; color:#409eff; font-weight:bold;">修改服务信息</div>
    </template>

    <el-form :model="row" ref="formRef" :rules="formRules" label-width="100px">
      <el-form-item label="主版本号：" prop="name">
        <el-input v-model="row.name"></el-input>
      </el-form-item>
      <el-form-item label="备注：">
        <el-input v-model="row.note" type="textarea"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogClose">取消</el-button>
        <el-button type="primary" @click="dialogDepartmentEdit_btn">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
export default {
  name: 'ServerEdit',
  // 介绍父组件的值
  props: {
    visible: Boolean, // 获取dialog是否打开变量
    row: {
      type: [Object,String,Number],
      default: 0
    }
  },
  data() {
    return {
      formRules: {
        name: [
          { required: true, message: '请输入服务名称', trigger: 'blur' },
          { min: 2, message: '服务名称长度应不小于2个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    // 点击关闭，子组件通知父组件更新属性
    dialogClose() {
      this.$emit('update:visible', false) // 父组件必须使用 v-model
    },
    dialogDepartmentEdit_btn() {
      // 验证表单是否通过
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$http
            .put('api/master_version/' + this.row.id + '/', this.row)
            .then(res => {
              if (res.data.code == 200) {
                // 反馈请求接口情况
                this.$message.success('修改数据成功')
                // 关闭弹出窗口
                this.dialogClose()
              } else {
                this.$message.info('修改数据失败')
              }
            })
            .catch(error => {
              this.$message.error('服务服务端接口请求错误！' + error)
            })
        } else {
          console.log("格式错误")
        }
      })
    }
  }
}
</script>

<style scoped></style>
